<?php
include_once("../debug.php");
include_once("../functions.php");

$db_name =  "vrwebmonitor";
$dbcnx = connect_db();

	//if ( ($pwd==$password) && (($permitflag==1)||($permitflag==2)) || $_GET["fr"] == "ajax" )

	$x = "x";
	$y = "y";
	$get_var;
	$exec;
	if ( isset($_GET["topol"]) )
	{
		$get_var = $_GET["topol"];
		if ( "ospf" == $get_var )
		{
				//在数据库中取ospf拓扑的坐标
				$x = "x2";
				$y = "y2";
		}
		else 
		{
				//在数据库中取bgp拓扑的坐标
				$x = "x3";
				$y = "y3";
		}
	}
	//echo $_GET["topol"] . "\n";

	$table_name = "vrlist";
	$exec = "SELECT vraddr, location FROM $table_name;";
	$map_res = mysql_query($exec);
	//IPv6地址和地理位置的映射
	$addr_loc_map = array();
	while($rec=mysql_fetch_array($map_res))
	{
		$addr_loc_map[strtolower($rec["vraddr"])] = $rec["location"];
	}

	$table_name = "locationmap";
	$exec = "select * from $table_name;";
	$loc_result = mysql_query($exec);
	//地理位置和地图坐标的映射
	$loc_map = array();
	while ($line = mysql_fetch_array($loc_result))
	{
		$tmp_loc .= "\t\t<location name = \"" . $line["location"] . "\">\n";
		$tmp_loc .= "\t\t\t<x>" . $line[$x] . "</x>\n";
		$tmp_loc .= "\t\t\t<y>" . $line[$y] . "</y>\n";
		$tmp_loc .= "\t\t</location>\n";
		$loc_map[strtolower($line["location"])] = $tmp_loc;
		$tmp_loc = "";
	}

	$table_name = "vrlinkstate";
	$exec = "SELECT * FROM $table_name;";
	$vlink_result = mysql_query($exec);
	$tab = array(array());
	//以xml格式保存所有vlink
	$vlink_xml = array();
	$i = 0;
	while ($row=mysql_fetch_array($vlink_result))
	{
		$tab[$i] = $row;
		$i++;
	}

	foreach ($tab as $key => $value)
	{
		$loc_addr1 = $value["vraddr"] . $value["locipv4"];
		$opp_addr1 = $value["oppipv6"] . $value["oppipv4"];
		foreach ($tab as $k => $v)
		{
			$loc_addr2 = $v["vraddr"] . $v["locipv4"];
			$opp_addr2 = $v["oppipv6"] . $v["oppipv4"];
			if ($loc_addr1 == $opp_addr2 && $loc_addr2 == $opp_addr1)
			{
				//echo $opp_addr;
				//echo $loc_addr . "\n";
				$tmp_xml .= "\t\t<vlink>\n";
				$tmp_xml .= "\t\t\t<vlinkid>" . $value["vlinkid"] . "</vlinkid>\n";
				$tmp_xml .= "\t\t\t<oppipv6>" . $value["oppipv6"] . "</oppipv6>\n";
				$tmp_xml .= "\t\t\t<locipv4>" . $value["locipv4"] . "</locipv4>\n";
				$tmp_xml .= "\t\t\t<oppipv4>" . $value["oppipv4"] . "</oppipv4>\n";
				$tmp_xml .= "\t\t\t<linkstate>" . $value["linkstate"] . "</linkstate>\n";
				$tmp_xml .= "\t\t\t<gohops>" . $value["gohops"] . "</gohops>\n";
				$tmp_xml .= "\t\t\t<comehops>" . $value["comehops"] . "</comehops>\n";
				$tmp_xml .= "\t\t\t<rtt>" . $value["rtt"] . "</rtt>\n";
				$tmp_xml .= "\t\t\t<sendinterval>" . $value["sendinterval"] . "</sendinterval>\n";
				$tmp_xml .= $loc_map[strtolower($addr_loc_map[strtolower($value["oppipv6"])])];
				//echo $loc_map[strtolower($addr_loc_map[$value["oppipv6"]])];
				$tmp_xml .= "\t\t</vlink>\n";
				//echo $tmp_xml;
				$vlink_xml[$value["vraddr"]][$value["locipv4"]] = $tmp_xml;
				$tmp_xml = "";
				//每个线段只有两个端点，找到了一个就可以结束了
				break;
			}
		}
	}
	//var_dump($tab);
	//var_dump($vlink_xml);
	
	if (defined(DEBUG))
	{

		echo "<pre>";
		var_dump($vlink_xml);
		echo "</pre>";
	}
	//最终向客户端输出的xml
	$xml_data = "";//"<?xml version=\"1.0\" encoding=\"UTF-8\"?..>\n";
	$xml_data .= "<topology>\n";

	$table_name = "vrlist";
	if ( "bgp" == $get_var )
	{
		$exec = "SELECT * FROM $table_name where location in (\"Dongda\", \"Beiyou\", \"Qinghua1\", \"Qinghua3\")";
	}
	else
	{
		$exec = "SELECT * FROM $table_name";
	}
	$vr_result = mysql_query($exec);
	while($record=mysql_fetch_array($vr_result))
	{
		$xml_data .= "\t<vr>\n";
		$xml_data .= "\t\t<vraddr>" . $record["vraddr"] . "</vraddr>\n";
		$xml_data .= "\t\t<port>" . $record["port"] . "</port>\n";
		$xml_data .= "\t\t<numofvlink>" . $record["numofvlink"] . "</numofvlink>\n";
		//如果到这个路由器上的所有vlink都up了，那么也可以把路由器的状态设为up，为了能更快的更新网页
		$xml_data .= "\t\t<linkstate>" . $record["linkstate"] . "</linkstate>\n";
		$xml_data .= $loc_map[strtolower($record["location"])];
		foreach ( (array)$vlink_xml[strtolower($record["vraddr"])] as $key => $val )
		{
			$xml_data .= $val;
			if (defined(DEBUG)) {
			echo $val;
			var_dump($val);
			}
		}
		$xml_data .= "\t</vr>\n";
	}
	$xml_data .= "</topology>\n";
	echo $xml_data;

?>

